﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;
using TheOutfield.UmbExt.UniversalMediaPicker.Interfaces;
using umbraco.IO;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Controls
{
    public abstract class AbstractOauthConfigControl : AbstractConfigControl
    {
        public override Control Control
        {
            get
            {
                return this;
            }
        }

        public abstract IOauthConfig OauthConfig { get; }
        public abstract string ModalWidth { get; }
        public abstract string ModalHeight { get; }
        public abstract string ProviderAlias { get; }
        public LiteralControl TokenLiteralControl = new LiteralControl();
        public TextBox TokenTextBox = new TextBox();
        public Control Container = new Control();

        public virtual string DialogUrl
        {
            get { return IOHelper.ResolveUrl(SystemDirectories.Umbraco) + string.Format("/plugins/theoutfield/universalmediapicker/universalmediapickeroauth.aspx?providerAlias={0}&clientIdCallBack={1}", ProviderAlias, TokenTextBox.ClientID); }
        }

        public virtual string CallbackUrl
        {
            get
            {
                string host = System.Web.HttpContext.Current.Request.Url.Host;
                string path = IOHelper.ResolveUrl(SystemDirectories.Umbraco) + "/plugins/theoutfield/universalmediapicker/universalmediapickeroauth.aspx";
                return string.Concat("http://", host, path);
            }
        }

        public virtual string CreateModalLink
        {
            get
            {
                string window = string.Format("javascript:window.open('{0}', 'Authentication', 'scrollbars=no,resizable=yes,menubar=no,width={1},height={2}');return false;", DialogUrl, ModalWidth, ModalHeight);
                return string.Format(" - <a href=\"#\" onclick=\"{0}\">Click to authenticate</a>", window);
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            TokenLiteralControl.Text = CreateModalLink;

            if (!Page.IsPostBack)
            {
                TokenTextBox.Text = !string.IsNullOrEmpty(OauthConfig.Token) ? OauthConfig.Token : "";
            }
        }

        protected override void CreateChildControls()
        {
            TokenLiteralControl.ID = "oAuthTokenLiteral";
            TokenTextBox.ID = "oAuthTokenTextBox";

            TokenTextBox.CssClass = "guiInputText guiInputStandardSize";

            /*Controls.Add(TokenLiteralControl);
            Controls.Add(TokenTextBox);*/

            Container.Controls.Add(TokenTextBox);
            Container.Controls.Add(TokenLiteralControl);

            Controls.Add(Container);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            /*writer.AddFormRow("oAuth authentication:", "Get Token", TokenLiteralControl);
            writer.AddFormRow("oAuth authentication:", "Authenticated Token", TokenTextBox);*/

            writer.AddFormRow("oAuth authentication:", "Authenticated Token", Container);
        }
    }
}